package com.qunar.qcf.handler;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.qunar.qcf.core.service.IoHandlerAdapter;
import com.qunar.qcf.core.session.IdleStatus;
import com.qunar.qcf.core.session.IoSession;
import com.qunar.qcf.socket.nio.NioSocketConnector;

/**
 * 抽象父类
 * 
 * @author zhangwan
 * @email wan.zhang@qunar.com
 */
public abstract class AbstractClientHandler extends IoHandlerAdapter {
	protected NioSocketConnector connector;
	public final Map<String, ResultHandler> ret = new ConcurrentHashMap<String, ResultHandler>();

	public void setConnector(NioSocketConnector connector) {
		this.connector = connector;
	}

	protected abstract void processor(IoSession session, Object message);

	@Override
	@Deprecated
	public final void sessionOpened(IoSession session) throws Exception {
	}
	
	@Override
	@Deprecated
	public final void exceptionCaught(IoSession session, Throwable cause) throws Exception {
	}

	@Override
	@Deprecated
	public final void messageReceived(IoSession session, Object message) throws Exception {
		processor(session, message);
		//该方法通过调用ExecutorService的shutdown()方法停止业务分发处理线程，
		//并设置内部disposed标志位标识需要停止连接管理器；
		//Worker线程通过该标识停止。
		connector.dispose();
	}

	@Override
	@Deprecated
	public final void sessionCreated(IoSession session) throws Exception {
	}

	@Override
	@Deprecated
	public final void sessionIdle(IoSession session, IdleStatus status) throws Exception {
	}

	@Override
	@Deprecated
	public final void sessionClosed(IoSession session) {
	}

	@Override
	@Deprecated
	public final void messageSent(IoSession session, Object message) throws Exception {
	}

}
